package com.ydcycloud.job.controller;


import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.mvc.model.Pagination;
import com.smart.mvc.model.Result;
import com.smart.mvc.validator.Validator;
import com.smart.mvc.validator.annotation.ValidateParam;
import com.smart.util.StringUtils;
import com.ydcycloud.job.model.ComProfession;
import com.ydcycloud.job.service.JobService;
import com.ydcycloud.sys.controller.BaseAdminController;

@Controller
@RequestMapping("/admin/job")
public class JobController extends BaseAdminController {

	@Resource
	private JobService jobService;

	/**
	 * 跳转到职业列表页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping
	public String excute(Model model) {
		return "/admin/job/jobList";
	}
	
	
	/**
	 * 职业列表
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(path = "/list")
	public @ResponseBody
	Result list(
			@ValidateParam(name = "开始页码", validators = { Validator.NOT_BLANK }) Integer pageNo,
			@ValidateParam(name = "显示条数 ", validators = { Validator.NOT_BLANK }) Integer pageSize,
			Integer areaCode, String jobName,String profession,String sortType,Integer companyId
			) {
		Pagination<Map<String, Object>> p =jobService.findJob(pageNo, pageSize, areaCode, jobName, profession, sortType, companyId);
		return Result.createSuccessResult().setData(p);
	}

	
	/**
	 * 删除职业
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/delete")
	public @ResponseBody Result delete(@ValidateParam(name="主键ids")String ids){
		if(StringUtils.isBlank(ids)){
			return Result.createErrorResult().setMessage("至少选择一行");
		}
		jobService.deleteByIds(ids);
		return Result.createSuccessResult().setMessage("删除成功");
	}
	
	/**
	 * 跳转到编辑/新增职业页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/edit")
	public String edit(Integer id, Model model) {
		if (id != null) {
			ComProfession bean = jobService.get(id);
			model.addAttribute("bean", bean);
		}
		model.addAttribute("list",jobService.findProfessionName());
		return "/admin/job/jobEdit";
	}
	
	/**
	 * 保存编辑职业
	 * @param request
	 * @param id
	 * @param tradeName
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody
	Result save(HttpServletRequest request, Integer id,String jobName,Integer professionId,Integer expTime,Integer education,
			Date endDate,Integer personCount,Double salary,String introduce,String requirement,Integer companyId) {
		
		if(StringUtils.isBlank(jobName)){
			return Result.createErrorResult().setMessage("职位名称不能为空");
		}
		
		ComProfession bean = null;
		if (id != null) {
			bean = jobService.get(id);
		} else {
			bean = new ComProfession();
			bean.setCreateBy(getSessionAdmin(request.getSession()).getName()); // 创建人
			bean.setCreateTime(new Date()); // 创建时间
		}
		bean.setJobName(jobName);
		bean.setProfessionId(professionId);
		bean.setExpTime(expTime);
		bean.setEducation(education);
		bean.setEndDate(endDate);
		bean.setPersonCount(personCount);
		bean.setSalary(salary);
		bean.setIntroduce(introduce);
		bean.setRequirement(requirement);
		bean.setCompanyId(companyId);
		bean.setLastUpdateTime(new Date());
		jobService.save(bean);
		return Result.createSuccessResult().setData(bean);
	}
	
	/**
	 * 跳转到编辑/新增职业页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/edits")
	public String edits(Integer id, Model model) {
		if (id != null) {
			ComProfession bean = jobService.get(id);
			model.addAttribute("bean", bean);
		}
		model.addAttribute("list",jobService.findProfessionName());
		return "/admin/job/jobEdits";
	}
	
	/**
	 * 保存编辑职业
	 * @param request
	 * @param id
	 * @param tradeName
	 * @return
	 */
	@RequestMapping(value = "/saves", method = RequestMethod.POST)
	public @ResponseBody
	Result saves(HttpServletRequest request, Integer id,String jobName,Integer professionId,Integer expTime,Integer education,
			Date endDate,Integer personCount,Double salary,String introduce,String requirement,Integer companyId) {
		
		if(StringUtils.isBlank(jobName)){
			return Result.createErrorResult().setMessage("职位名称不能为空");
		}
		
		ComProfession bean = null;
		if (id != null) {
			bean = jobService.get(id);
		} else {
			bean = new ComProfession();
			bean.setCreateBy(getSessionAdmin(request.getSession()).getName()); // 创建人
			bean.setCreateTime(new Date()); // 创建时间
		}
		bean.setJobName(jobName);
		bean.setProfessionId(professionId);
		bean.setExpTime(expTime);
		bean.setEducation(education);
		bean.setEndDate(endDate);
		bean.setPersonCount(personCount);
		bean.setSalary(salary);
		bean.setIntroduce(introduce);
		bean.setRequirement(requirement);
		bean.setCompanyId(companyId);
		bean.setLastUpdateTime(new Date());
		jobService.save(bean);
		return Result.createSuccessResult().setData(bean);
	}

}
